package main

import (
	"sdl"
	"rand"
)

type Obj struct {
    x int16
    y int16
    dx int16
    dy int16
    ax int16
    ay int16
    color uint32
    width uint16
    height uint16
}

func (o *Obj) Update() {
    o.x += o.dx
    o.y += o.dy
    if o.x < 0 {
        o.x = 0
        o.dx = -o.dx
    }
    if o.y < 0 {
        o.y = 0
        o.dy = -o.dy
    }
    if o.x + (int16)(o.width << 2) > (640 << 2) {
        o.x = (640 - (int16)(o.width)) << 2
        o.dx = -o.dx
    }
    if o.y + (int16)(o.height << 2) > (480 << 2) {
        o.y = (480 - (int16)(o.height)) << 2
        o.dy = -o.dy
    }
    o.dx += o.ax
    o.dy += o.ay
    var lim int16 = 1 << 2
    if o.dx < -lim { o.dx = -lim }
    if o.dx > lim { o.dx = lim }
    if o.dy < -lim { o.dy = -lim }
    if o.dy > lim { o.dy = lim }
    o.ax += int16(rand.Intn(11) - 5)
    o.ay += int16(rand.Intn(11) - 5)
    lim = 1 << 2
    if o.ax < -lim { o.ax = -lim }
    if o.ax > lim { o.ax = lim }
    if o.ay < -lim { o.ay = -lim }
    if o.ay > lim { o.ay = lim }
}

func (o *Obj) Reset() {
    o.width = 3
    o.height = 3
    o.x = int16(rand.Intn((int)(640 - o.width) << 2))
    o.y = int16(rand.Intn((int)(480 - o.height) << 2))
    o.dx = int16(rand.Intn(5) - 2) << 2
    o.dy = int16(rand.Intn(5) - 2) << 2
    o.ax = int16(rand.Intn(3) - 1) << 2
    o.ay = int16(rand.Intn(3) - 1) << 2
    // var c int = rand.Intn(156) + 100
    // o.color = (uint32)((c << 16) | (c << 8) | c)
    o.color = rand.Uint32()
}

type BoxDrawTest struct {
	objs [100]*Obj
}

func (m *BoxDrawTest) Activate() {
	for i, _ := range m.objs { m.objs[i] = &Obj{} }
	for _, obj := range m.objs {
		obj.Reset()
	}
}

func (m *BoxDrawTest) Update() {
	for _, obj := range m.objs {
		obj.Update()
		Screen.FillRect(&sdl.Rect{obj.x >> 2, obj.y >> 2, obj.width, obj.height}, obj.color)
	}
}

func (m *BoxDrawTest) Deactivate() {
}

